package the_fourth_chapter;

public class Insert_Sort2 {

	public static void main(String[] args) {
		int[] nums = { 4, 5, 6, 3, 2, 8, 2, 9, 1 };
		System.out.println("传入的数组为：");
		for (int i = 0; i < nums.length; i++) {
			System.out.print(nums[i] + "\t");
		}

		System.out.println();

		System.out.println("经过排序后的数组为：");
		insert_sort(nums, nums.length - 1);
		for (int i = 0; i < nums.length; i++) {
			System.out.print(nums[i] + "\t");
		}
	}

	public static void insert_sort(int[] nums, int length) {
		if (length == 0) {
			return;
		}
		insert_sort(nums, length - 1);
		insert(nums, length);
	}

	public static void insert(int[] nums, int j) {
		if (j > 0 && nums[j] < nums[j - 1]) {
			swap(nums, j, j - 1);
			insert(nums, j - 1);
		}
	}

	public static void swap(int[] nums, int i, int j) {
		int temp;
		temp = nums[i];
		nums[i] = nums[j];
		nums[j] = temp;
	}

}
